home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 2 of 3.iso / chapte10 / clienttr.c < prev    next >
C/C++ Source or Header  |  1996-03-06  |  9KB  |  259 lines

  1.  
  2. #include <windows.h>  
  3. #include "clienttr.h"  
  4.  
  5.  
  6. #if defined (WIN32)
  7.     #define IS_WIN32 TRUE
  8. #else
  9.     #define IS_WIN32 FALSE
  10. #endif
  11.  
  12. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  13. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  14. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  15.  
  16. HINSTANCE hInst;   // current instance
  17.  
  18. LPCTSTR lpszAppName = "MyApp";
  19. LPCTSTR lpszTitle   = "DdeClientTransaction()"; 
  20.  
  21.  
  22. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  23.  
  24.  
  25. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  26.                       LPTSTR lpCmdLine, int nCmdShow)
  27. {
  28.    MSG      msg;
  29.    HWND     hWnd; 
  30.    WNDCLASS wc;
  31.  
  32.    wc.style         = CS_HREDRAW | CS_VREDRAW;
  33.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  34.    wc.cbClsExtra    = 0;                      
  35.    wc.cbWndExtra    = 0;                      
  36.    wc.hInstance     = hInstance;              
  37.    wc.hIcon         = LoadIcon (hInstance, lpszAppName); 
  38.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  39.    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  40.    wc.lpszMenuName  = lpszAppName;              
  41.    wc.lpszClassName = lpszAppName;              
  42.  
  43.    if ( IS_WIN95 )
  44.    {
  45.       if ( !RegisterWin95( &wc ) )
  46.          return( FALSE );
  47.    }
  48.    else if ( !RegisterClass( &wc ) )
  49.       return( FALSE );
  50.  
  51.    hInst = hInstance; 
  52.  
  53.    hWnd = CreateWindow( lpszAppName, 
  54.                         lpszTitle,    
  55.                         WS_OVERLAPPEDWINDOW, 
  56.                         CW_USEDEFAULT, 0, 
  57.                         CW_USEDEFAULT, 0,  
  58.                         NULL,              
  59.                         NULL,              
  60.                         hInstance,         
  61.                         NULL               
  62.                       );
  63.  
  64.    if ( !hWnd ) 
  65.       return( FALSE );
  66.  
  67.    ShowWindow( hWnd, nCmdShow ); 
  68.    UpdateWindow( hWnd );         
  69.  
  70.    while( GetMessage( &msg, NULL, 0, 0) )   
  71.    {
  72.       TranslateMessage( &msg ); 
  73.       DispatchMessage( &msg );  
  74.    }
  75.  
  76.    return( msg.wParam ); 
  77. }
  78.  
  79.  
  80. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  81. {
  82.    WNDCLASSEX wcex;
  83.  
  84.    wcex.style         = lpwc->style;
  85.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  86.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  87.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  88.    wcex.hInstance     = lpwc->hInstance;
  89.    wcex.hIcon         = lpwc->hIcon;
  90.    wcex.hCursor       = lpwc->hCursor;
  91.    wcex.hbrBackground = lpwc->hbrBackground;
  92.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  93.    wcex.lpszClassName = lpwc->lpszClassName;
  94.  
  95.    // Added elements for Windows 95.
  96.    //...............................
  97.    wcex.cbSize = sizeof(WNDCLASSEX);
  98.    wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName, 
  99.                             IMAGE_ICON, 16, 16,
  100.                             LR_DEFAULTCOLOR );
  101.             
  102.    return RegisterClassEx( &wcex );
  103. }
  104.  
  105. HDDEDATA CALLBACK DdemlCallback( UINT     uType,
  106.                                  UINT     uFmt,    
  107.                                  HCONV    hconv,    
  108.                                  HSZ      hsz1,    
  109.                                  HSZ      hsz2,    
  110.                                  HDDEDATA hdata,    
  111.                                  DWORD    dwData1,
  112.                                  DWORD    dwData2 );
  113.  
  114.  
  115. DWORD ddeInst    = 0;
  116. HSZ   hszTopic   = NULL;
  117. HSZ   hszService = NULL;
  118. HCONV hConv      = NULL;
  119.  
  120. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  121. {
  122.    switch( uMsg )
  123.    {
  124.       case WM_CREATE :
  125.               // Initialize DDEML.
  126.               //..................
  127.               if ( DdeInitialize( &ddeInst, DdemlCallback, 
  128.                    APPCMD_CLIENTONLY, 0 ) != DMLERR_NO_ERROR )
  129.               {
  130.                  MessageBox( hWnd, "Could not initialize DDEML.", 
  131.                              NULL, MB_OK | MB_ICONSTOP );
  132.                  return( -1 );
  133.               }
  134.  
  135.               // Allocate strings.
  136.               //..................
  137.               hszService = DdeCreateStringHandle( ddeInst,"Server",CP_WINANSI );
  138.               hszTopic   = DdeCreateStringHandle( ddeInst,"Msg",CP_WINANSI );
  139.  
  140.               // Connect to server.
  141.               //...................
  142.               hConv = DdeConnect( ddeInst, hszService, 
  143.                                   hszTopic, NULL );
  144.  
  145.               if ( DdeGetLastError( ddeInst ) != DMLERR_NO_ERROR )
  146.                  MessageBox( hWnd, "Could not connect to server.", 
  147.                                    NULL, MB_OK | MB_ICONSTOP );
  148.  
  149.  
  150.               break;
  151.  
  152.       case WM_COMMAND :
  153.               switch( LOWORD( wParam ) )
  154.               {
  155.                  case IDM_TEST :
  156.                         if ( hConv )
  157.                         {
  158.                            // Execute a DDE Transaction on the 
  159.                            // DDEML server in synchronous mode.
  160.                            //...................................
  161.                            HDDEDATA hRet = DdeClientTransaction( 
  162.                                                  "This is a sample message",
  163.                                                  37, hConv, 0L, 0, 
  164.                                                  XTYP_EXECUTE,
  165.                                                  30000,
  166.                                                  NULL ); 
  167.  
  168.                            if ( !hRet )
  169.                            {
  170.                               UINT   uError  = DdeGetLastError( ddeInst );
  171.                               LPTSTR lpError = NULL;
  172.  
  173.                               switch( uError )
  174.                               {
  175.                                  case DMLERR_BUSY :  
  176.                                           lpError = "The server is busy."; break;
  177.  
  178.                                  case DMLERR_EXECACKTIMEOUT    :
  179.                                           lpError = "The transaction timed out."; break;
  180.  
  181.                                  case DMLERR_NOTPROCESSED :
  182.                                           lpError = "The transaction was not processed."; break;
  183.  
  184.                                  case DMLERR_REENTRANCY :
  185.                                           lpError = "Still waiting for a response from server."; break;
  186.  
  187.                                  default :
  188.                                           lpError = "An error occured."; break;
  189.                               }
  190.                               
  191.                               MessageBox( hWnd, lpError, lpszAppName, MB_OK | MB_ICONASTERISK );
  192.                            }
  193.                         }
  194.                         break;
  195.  
  196.                  case IDM_ABOUT :
  197.                         DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
  198.                         break;
  199.  
  200.                  case IDM_EXIT :
  201.                         DestroyWindow( hWnd );
  202.                         break;
  203.               }
  204.               break;
  205.       
  206.       case WM_DESTROY :
  207.               if ( hConv )
  208.                  DdeDisconnect( hConv );
  209.                   
  210.               DdeFreeStringHandle( ddeInst, hszService );
  211.               DdeFreeStringHandle( ddeInst, hszTopic );
  212.               DdeUninitialize( ddeInst );
  213.               PostQuitMessage(0);
  214.               break;
  215.  
  216.       default :
  217.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  218.    }
  219.  
  220.    return( 0L );
  221. }
  222.  
  223.  
  224. HDDEDATA CALLBACK DdemlCallback( UINT     uType,
  225.                                  UINT     uFmt,    
  226.                                  HCONV    hconv,    
  227.                                  HSZ      hsz1,    
  228.                                  HSZ      hsz2,    
  229.                                  HDDEDATA hdata,    
  230.                                  DWORD    dwData1,
  231.                                  DWORD    dwData2 )
  232. {
  233.    return( NULL );
  234. }
  235.  
  236.  
  237. LRESULT CALLBACK About( HWND hDlg,           
  238.                         UINT message,        
  239.                         WPARAM wParam,       
  240.                         LPARAM lParam)
  241. {
  242.    switch (message) 
  243.    {
  244.        case WM_INITDIALOG: 
  245.                return (TRUE);
  246.  
  247.        case WM_COMMAND:                              
  248.                if (   LOWORD(wParam) == IDOK         
  249.                    || LOWORD(wParam) == IDCANCEL)    
  250.                {
  251.                        EndDialog(hDlg, TRUE);        
  252.                        return (TRUE);
  253.                }
  254.                break;
  255.    }
  256.  
  257.    return (FALSE); 
  258. }
  259.